import storage from '@/store/storage'
import req from '@/assets/js/req';

const user = {
  state: {
  },
  mutations: {
    BIND_LOGIN(state, loginfo) {
      storage.setUser(loginfo.user);
      // storage.setToken(loginfo.token);
      storage.setDict(loginfo.dicts);
      storage.setMenus(loginfo.menus);
      storage.setPowers(loginfo.powers);
      storage.setConfigs(loginfo.configs)
    },
    BIND_LOGOUT: (state) => {
      storage.removeUser();
      storage.removeDict();
      storage.delToken();
      storage.removeMenus();
      storage.removePowers();
      storage.removeConfigs();
    }
  },
  actions: {
    bindLogin({ commit }, loginfo) {
      commit('BIND_LOGIN', loginfo)
    },
    bindLogout({ commit }, args) {
      if (!storage.getLoginState()) {
        args.callback && args.callback(1);
      } else {
        storage.setLoginState(false); // 标记退出状态

        let param = new FormData();
        param.append('token', args.token);

        req.post(param, api.logout, function (_r) {
          commit('BIND_LOGOUT', args.token)

          args.callback && args.callback(2);
        });
      }
    }
  }
}

export default user
